package com.tengniu.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tengniu.biz.bo.BannerResultBo;
import com.tengniu.api.service.BannerApiService;
import com.tengniu.buss.domain.Banner;
import com.tengniu.buss.service.BannerService;
import com.tengniu.common.buss.enums.YesOrNoEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BannerApiServiceImpl implements BannerApiService {

    @Resource
    private BannerService bannerService;

    @Override
    public List<BannerResultBo> banners() {
        List<Banner> banners = bannerService.list(Wrappers.lambdaQuery(Banner.class)
                .eq(Banner::getStatus, YesOrNoEnum.YES.getCode())
                .orderByDesc(Banner::getSort, Banner::getCreateTime));
        return banners.stream().map(banner -> {
            BannerResultBo bannerResultBo = new BannerResultBo();
            BeanUtils.copyProperties(banner, bannerResultBo);
            return bannerResultBo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<BannerResultBo> bannersByType(Integer type) {
        List<Banner> banners = bannerService.list(Wrappers.lambdaQuery(Banner.class)
                .eq(Banner::getStatus, YesOrNoEnum.YES.getCode())
                .eq(Banner::getType, type)
                .orderByDesc(Banner::getSort, Banner::getCreateTime));
        return banners.stream().map(banner -> {
            BannerResultBo bannerResultBo = new BannerResultBo();
            BeanUtils.copyProperties(banner, bannerResultBo);
            bannerResultBo.setCreateTime(banner.getCreateTime());
            return bannerResultBo;
        }).collect(Collectors.toList());
    }
}
